//
//  MPCTextView.m
//  Pods
//
//  Created by HouGeng on 17/7/22.
//
//

#import "MPCTextView.h"
#import "MPCLabel.h"
#import "MPToolsHeader.h"

/// 文本框描边颜色
#define MPt_TextView_BorderColor [UIColor redColor]

/// 文本框默认文字颜色
#define MPT_TextView_PlaceholderColor [UIColor blackColor]

/// 文本框圆角大小
#define MPT_TextView_CornerRadius (2.0)

/// 文本框文字字体
#define MPT_TextView_Font [UIFont systemFontOfSize:(15.0)]

@interface MPCTextView ()


/// 显示默认文字的label
@property (nonatomic, strong) MPCLabel *labPlaceholder;


@end


@implementation MPCTextView


#pragma mark - *********************************** View Lifecycle **********************************

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        /// 设置公共属性，与APP的UI规范 无关
        [self setCommonAttribute];
        
        /// 设置定制属性，与APP的UI规范 有关，暂不用
        //[self setCustomAttribute];
        
        /// 初始化变量
        [self initVariable];
    }
    
    return self;
}

/// 初始化变量
- (void)initVariable
{
    if (self)
    {
        /// 注册通知，监听文本输入开始、结束、文本变化状态
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(TPTextViewBeginEditing:)
                                                     name:UITextViewTextDidBeginEditingNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(TPTextViewEndEditing:)
                                                     name:UITextViewTextDidEndEditingNotification
                                                   object:nil];
        
        /// 初始化显示占位符的label
        [self initPlacehodlerLabel];
    }
}

/// 设置公共属性，与APP的UI规范 无关
- (void)setCommonAttribute
{
    if (self)
    {
        /// 设置默认字体
        self.font = MPT_TextView_Font;
        /// 设置背景色为透明色
        self.backgroundColor = [UIColor clearColor];
    }
}

/// 设置定制属性，与APP的UI规范 有关
- (void)setCustomAttribute
{
    if (self)
    {
        
    }
}

/// 初始化显示占位符的label
- (void)initPlacehodlerLabel
{
    /// TODO:颜色需要和UED确认
    _labPlaceholder = [MPCLabel getWithFrame:CGRectMake(5, 5, self.contentSize.width - 10, 30)
                                       text:nil
                                  textColor:MPT_TextView_PlaceholderColor
                                   textFont:MPT_TextView_Font];
    _labPlaceholder.numberOfLines = 0;
    _labPlaceholder.hidden = NO;
    //    _labPlaceholder.enabled = NO;
    [self addSubview:_labPlaceholder];
}

- (void)dealloc
{
    /// 移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - ********************************** 初始化方法 工厂方法 *********************************

/// 创建一个普通的textView
+ (MPCTextView *)getWithFrame:(CGRect)frame target:(id)target
{
    return [[MPCTextView alloc] initWithFrame:frame target:target];
}

/// 创建一个textview，设置默认占位文字
+ (MPCTextView *)getWithFrame:(CGRect)frame target:(id)target
                 placeholder:(NSString *)strPlaceholder
{
    return [[MPCTextView alloc] initWithFrame:frame target:target
                                 placeholder:strPlaceholder];
}

#pragma mark - ********************************** 初始化方法 实例方法 *********************************

/// 实例化一个textview，设置代理
- (instancetype)initWithFrame:(CGRect)frame target:(id)target
{
    if (self = [self initWithFrame:frame])
    {
        self.delegate = target;
    }
    
    return self;
}

/// 实例化一个textview，设置代理、默认文字
- (instancetype)initWithFrame:(CGRect)frame target:(id)target
                  placeholder:(NSString *)strPlaceholder
{
    if (self = [self initWithFrame:frame])
    {
        self.delegate = target;
        
        /// 设置默认文字
        [self setPlaceholder:strPlaceholder];
    }
    
    return self;
}

/// 设置圆角、边框
- (void)setCornerRadius:(CGFloat)fltCornerRadius
            borderWidth:(CGFloat)fltBorderWidth
            borderColor:(UIColor *)colorBorder
{
    /// 设置边框和圆角
    self.layer.cornerRadius = fltCornerRadius;
    self.layer.borderColor = [colorBorder CGColor];
    self.layer.borderWidth = fltBorderWidth;
    self.clipsToBounds = YES;
}

/// 设置默认占位文字
- (void)setPlaceholder:(NSString *)strPlaceholder
{
    if (!_labPlaceholder) /// 为空时进行初始化
    {
        [self initPlacehodlerLabel];
    }
    
    /// 设置默认文字，并改变默认文字label高度
    _labPlaceholder.text = strPlaceholder;
    [self resizePlaceholderLabel];
}

/// 设置默认占位文字的颜色
- (void)setPlaceholderColor:(UIColor *)colorPlaceholder
{
    if (colorPlaceholder)
    {
        if (!self.labPlaceholder) /// 为空时，先进行初始化
        {
            [self initPlacehodlerLabel];
        }
        
        self.labPlaceholder.textColor = colorPlaceholder;
    }
}

/// 设置默认占位文字的字体
- (void)setPlaceholderFont:(UIFont *)fontPlaceholder
{
    if (fontPlaceholder)
    {
        if (!self.labPlaceholder) /// 为空时，先进行初始化
        {
            [self initPlacehodlerLabel];
        }
        
        [self.labPlaceholder setFont:fontPlaceholder];
    }
}

/// 设置默认占位文字的对齐方式
- (void)setPlaceholderTextAlignment:(NSTextAlignment)textAlignment
{
    if (textAlignment)
    {
        if (!self.labPlaceholder) /// 为空时，先进行初始化
        {
            [self initPlacehodlerLabel];
        }
        
        [self.labPlaceholder setTextAlignment:textAlignment];
    }
}

/// 设置placeholder的frame
- (void)setPlaceholderFrame:(CGRect)rect
{
    _labPlaceholder.frame = rect;
}

/// 重写设置字体
- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    
    /// 设置输入文字字体，如果和当前字体一样，不再做设置
    if (font && ![font isEqual:self.font])
    {
        /// 默认文本字体设置为和输入文字字体相同，并改变label高度
        _labPlaceholder.font = font;
        [self resizePlaceholderLabel];
    }
}

/// 重写setText:
- (void)setText:(NSString *)text
{
    [super setText: text];
    
    /// 根据设置的文字内容判断是否显示默认文本
    _labPlaceholder.hidden = ([text isEqualToString:@""]) ? NO : YES;
}


#pragma mark - ************************************ Functions **************************************

/// 根据文字内容设置默认文字label的高度，计算文字高度的方法待修改
- (void)resizePlaceholderLabel
{
    NSAttributedString *abstr = [[NSAttributedString alloc] initWithString:MPT_Str_Protect(_labPlaceholder.text)
                                                                attributes:@{NSFontAttributeName:_labPlaceholder.font}];
    /// 计算文字size
    CGSize sizePlaceholder = [abstr boundingRectWithSize:CGSizeMake(_labPlaceholder.frame.size.width, CGFLOAT_MAX)
                                                 options:NSStringDrawingUsesLineFragmentOrigin
                                                 context:nil].size;
    
    /// 改变label高度
    _labPlaceholder.frame = CGRectMake(_labPlaceholder.frame.origin.x, _labPlaceholder.frame.origin.y, _labPlaceholder.frame.size.width, sizePlaceholder.height);
}

- (void)setPlaceholderViewHidden:(BOOL *)isHidden;
{
    self.labPlaceholder.hidden = isHidden;
}

- (void)notHelpManagePlaceholder
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
}

#pragma mark - ********************************** Notifications ************************************

/// 开始输入
- (void)TPTextViewBeginEditing:(NSNotification*)notification
{
    /// 暂时不做操作
    _labPlaceholder.hidden = YES;
}

/// 结束输入
- (void)TPTextViewEndEditing:(NSNotification*)notification
{
    /// 内容为空时，显示默认占位符
    _labPlaceholder.hidden = ([self.text isEqualToString:@""]) ? NO : YES;
}

@end
